<?php

@session_start();

class Product_listing extends Controller {

    function __construct() {
        parent::__construct();
    }

	
    function index() {
        // echo 'in';exit;
        $sql = new sql();
        $sql->connect();
        $this->view->category = $sql->fetchAll("tbl_category", array("cat_status" => 1));


        if (array_key_exists('1', getId())) {

            $GETID = getId();
            $prd_id = $GETID[1];
            $productArray = $sql->fetch("tbl_product", array("prd_id" => $prd_id,"prd_status"=>1));

            if ($productArray['prd_id'] == '') {
                header("Location: " . URL . "product");
            } else {
                // 
                $this->view->successMessage = "";
                $this->view->ErrorMessage = "";
				$this->view->quantity_numb = "1";
                //add cart
                if (isset($_POST['add_cart'])) {
					
				
				function IsEmpty($value='') {
					if (trim($value) == '') {
						return true;
					}
					return false;
				}
				function isvalidQuantity($n) {
					if (!preg_match("/^[0-9]+$/", $n)) {
						return false;
					}
					if ($n <= 0) {
						return false;
					}
					return true;
				}

					
					$errorMessage = Array();
					$postedData = $_POST;
					if (IsEmpty($_POST['quantity'])) {
						$errorMessage = "Quantity cannot be blank";
					} else {
						if (!isvalidQuantity($postedData['quantity'])) {
							$errorMessage = "Quantity should be a number greater than zero";
						}
					}
					
					if(count($errorMessage) > 0){
						$this->view->quantity_numb = $_POST['quantity'];
						$this->view->ErrorMessage = $errorMessage;
					}else{
						
                    $prd_id = $_POST['product_id'];
                    $productArray = $sql->fetch("tbl_product", array("prd_id" => $prd_id));
                    /* if(in_array($prd_id, $productArray)){
						$this->view->ErrorMessage = "Product already added.";
                      } */

                    $cart = $_SESSION['shoping_cart'];
                    $cnt = 0;
                    $product_qty = $_POST['quantity'];
                    if (count($cart) > 0) {

                        foreach ($cart as $subKey => $subArray) {
                            if ($subArray['prd_id'] == $productArray['prd_id']) {
                                $cart[$subKey]['prd_qty'] = $product_qty;//$cart[$subKey]['prd_qty'] + $product_qty;
                                $cnt++;
                            }
                        }
                        $_SESSION['shoping_cart'] = $cart;
                        header('Location: ' . URL . 'cart');
                    }
                    if ($cnt == 0) {

                        $cart[] = array(
                            'prd_id' => $productArray['prd_id'],
                            'prd_name' => $productArray['prd_name'],
                            'prd_qty' => $product_qty,
							'prd_shortdesc' => htmlspecialchars_decode($productArray['prd_short_desc']),
                            'prd_desc' => htmlspecialchars_decode($productArray['prd_description']),
                            'quick_book_id' => $productArray['quick_book_id'],
                            'prd_price' => $productArray['prd_price'],
                            'prd_image' => $productArray['prd_image'],
                            'prd_code' => $productArray['prd_code'],
                            'prd_status' => $productArray['prd_status'],
                        );
                        $_SESSION['shoping_cart'] = $cart;
                        header('Location: ' . URL . 'cart');
                        //$this->view->successMessage = "Product add to cart.";
                    }


					}
					
				
				
                    //header('Location: ' . URL . 'product_listing/' .$productArray['prd_id'].'&serverreply=' . urlencode($this->view->serverreply));

                    /* echo '<pre>';
                      print_R($_SESSION['shoping_cart']);
                      exit; */
                }
            }
        } else {
            header("Location: " . URL . "product");
        }

        $this->view->productArray = $productArray;
        $this->view->render('product_listing');
    }

}
